package com.atguigu.tingshu.order.pattern.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.pattern.TradeStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName : AlbumTrade
 * Package : com.atguigu.tingshu.order.pattern.impl
 * Description :
 * <p>
 * Author: LHZ
 * Create: 2025/8/4 15:15
 * Version: 1.0
 */
@Slf4j
@SuppressWarnings("all")
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumTrade implements TradeStrategy {

    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Override
    public OrderInfoVo trade(Long userId, TradeVo tradeVo) {
        //创建返回结果对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        //创建订单明细列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        //创建订单减免明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        //声明金额相关变量
        BigDecimal originPrice = new BigDecimal("0.00");
        BigDecimal discountPrice = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");

        //如果付款项目为专辑
        //调用远程服务查看该专辑是否已被此用户购买
        Boolean isPaid = userFeignClient.isPaidAlbum(tradeVo.getItemId()).getData();
        Assert.notNull(isPaid,"【用户服务】提供isPaidAlbum远程调用失败");
        if(isPaid) {
            throw new GuiguException(500, "用户已经购买此专辑");
        }
//            Assert.isFalse(isPaid,"用户已经购买此专辑{}",tradeVo.getItemId()); //等同上面的if判断
        //调用远程服务查看专辑相关信息(价格，折扣等)
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfoById(tradeVo.getItemId()).getData();
        Assert.notNull(albumInfo,"查看专辑失败,id:{}",tradeVo.getItemId());

        //设置价格信息
        originPrice = albumInfo.getPrice(); //原价
        //折扣信息
        BigDecimal discount = albumInfo.getDiscount();
        BigDecimal vipDiscount = albumInfo.getVipDiscount();
        //调用远程服务获取用户信息 并计算实际价格和优惠价格
        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(userId).getData();
        Assert.notNull(userInfoVo,"用户id:{}不存在",userId);
        Boolean isVIP = false;
        if(userInfoVo.getIsVip().intValue() == 1 && userInfoVo.getVipExpireTime().after(new Date())){
            isVIP = true;
        }
        //基于用户身份计算专辑实际价格
        if(isVIP && vipDiscount.doubleValue() != -1){
            //如果该用户是vip且专辑有折扣   "0.1-9.9  不打折 -1"
            discountPrice = originPrice.multiply(vipDiscount).divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
        }
        if(!isVIP && discount.doubleValue() != -1){
            //如果该用户不是vip且专辑有折扣  "0.1-9.9  不打折 -1"
            discountPrice = originPrice.multiply(discount).divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
        }
        //优惠价格
        derateAmount = originPrice.subtract(discountPrice);
        //声明订单详情对象 加入到订单明细列表
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(albumInfo.getId());
        orderDetailVo.setItemName("专辑: " + albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(originPrice);
        orderDetailVoList.add(orderDetailVo);

        //如果有优惠,封装订单减免vo对象,加入到订单减免列表
        if(originPrice.compareTo(discountPrice) == 1){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("专辑折扣减免: " + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }

        orderInfoVo.setItemType(tradeVo.getItemType()); //付款项目类型: 1001-专辑 1002-声音 1003-vip会员
        orderInfoVo.setOriginalAmount(originPrice);  //原价
        orderInfoVo.setDerateAmount(derateAmount);   //优惠价
        orderInfoVo.setOrderAmount(discountPrice);   //实际价
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        return orderInfoVo;
    }
}